import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../config/public_color.dart';
import '../../tool/debug.dart';

class Toast {
  static const defaultDurationSeconds = 2;

  static ToastGravity? defaultGravity = ToastGravity.CENTER;

  static Function(String? msg, int? toastSeconds, ToastGravity? gravity)?
      onToast;

  /// 重复内容默认不多次弹出
  static bool repeatShow = false;
  static String? currentMessage;

  static void show(
    String? message, {
    int? durationSeconds,
    ToastGravity? gravity,
  }) {
    if (message == null || message == '') {
      return;
    }
    if (!repeatShow) {
      if (currentMessage == message) {
        return;
      } else {
        currentMessage = message;
        Future.delayed(Duration(seconds: durationSeconds ?? defaultDurationSeconds), () {
          if (currentMessage == message) {
            currentMessage = null;
          }
        });
      }
    }
    debugLog('toast: $message');
    if (onToast != null) {
      onToast?.call(message, durationSeconds, gravity);
      return;
    }
    Fluttertoast.showToast(
        msg: message,
        gravity: gravity ?? defaultGravity,
        timeInSecForIosWeb: durationSeconds ?? defaultDurationSeconds,
        backgroundColor: PublicColor.mask,
        textColor: Colors.white,
        fontSize: 14.0);
  }

  static void dismiss() {
    Fluttertoast.cancel();
  }
}
